# This file Replicates Table 1 and Figures 3-5 of Goldstein and Gulotty
# gulotty@uchicago.edu
# August 24, 2020.

# It assumes you have two csvs, RegressionAnalysis.csv and borderMeasures.csv in directory.

library(tidyverse)
library(stargazer)
library(slider)


# Read in data from USITC.

df1<-read_csv("RegressionAnalysis.csv")

# Read in data on counts of Title VII

df2<-read_csv("borderMeasures.csv")

# I begin my analysis by lagging the two independent variables, 

# the case counts:

df1$Case_Commerce_lag<-c(NA, df1$Case_Commerce[-length(df1$Case_Commerce)])

# and the unemployment rates, entering the value for 1980:

df1$Unemployment_lag<-c(6, df1$Unemployment[-length(df1$Unemployment)])


# Four year trends:
df1$Unemployment4yr<-slide_dbl(df1$Unemployment_lag, ~ mean(.x, na.rm=T), .before=4)
df1$Case_Commerce4yr<-slide_dbl(df1$Case_Commerce_lag, ~ mean(.x, na.rm=T), .before=4)
df1$Failure_Rate4yr<-slide_dbl(df1$Failure_Rate, ~ mean(.x, na.rm=T), .before=4)

# Note, the lag of Case_commerce is not available in 1980, so we begin analysis in 1981

df1981_2016<-df1%>%filter(Year>1980)

summary(mod4a<-lm(Failure_Rate4yr~Case_Commerce4yr, data=df1981_2016))

summary(mod4b<-lm(Failure_Rate4yr~Unemployment4yr, data=df1981_2016))

summary(mod4c<-lm(Failure_Rate4yr~Unemployment4yr+Case_Commerce4yr, data=df1981_2016))

stargazer( mod4a, mod4b, mod4c)




# Plots

# Border measures

ggplot(df1)+
  geom_point(aes(Year, Case_Commerce_lag))+
  geom_line(aes(Year, Case_Commerce4yr))+theme_bw() +
  labs(y="Count of Cases", title="Decline in Title VII Cases")
#ggsave("CaseCount_Year.png")


#Get residuals rom regression:

modUnemp<-lm(Unemployment4yr~Case_Commerce4yr, data=df1981_2016)
modFailure<-lm(Failure_Rate4yr~Case_Commerce4yr, data=df1981_2016)

df1981_2016$Unemployment4yr_resid<-residuals(modUnemp)
df1981_2016$Failure_Rate4yr_resid<-residuals(modFailure)

#slightly shift 1997 and 1992 to solve an overplotting issue.

df1981_2016$Failure_Rate4yr_resid_move<-df1981_2016$Failure_Rate4yr_resid

df1981_2016[df1981_2016$Year==1997, "Failure_Rate4yr_resid_move"]<-df1981_2016[df1981_2016$Year==1997, "Failure_Rate4yr_resid"]-.015
df1981_2016[df1981_2016$Year==1992, "Failure_Rate4yr_resid_move"]<-df1981_2016[df1981_2016$Year==1992, "Failure_Rate4yr_resid"]+.01

ggplot(df1981_2016)+
  geom_smooth(aes(Unemployment4yr_resid,Failure_Rate4yr_resid),method="lm", color="grey", alpha=.5)+
  geom_text(aes(x=Unemployment4yr_resid,y=Failure_Rate4yr_resid_move, label=Year))+
  theme_bw()+
  labs(x="Unemployment Rate (4 yr ave., resid.)", y="Failure Rate (4 yr ave., resid.)", 
      title="Effect of Unemployment on Case Rejection, Controlling for # of Cases")

ggsave("Unemployment_Failure_reg.png")


df2long<-df2%>%
			select(Year:'Section 301')%>%
			gather("type", "count", -Year)

ggplot(df2long, aes(Year, count, fill=type))+
  geom_col()+
  theme_bw()+
  theme(legend.position="bottom")+
  labs(title="Persistance of Border Measures", fill="Auth.")
  
ggsave("Border_Measures_Figure.png", width=8, height=4)
